package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.ContractContain;
import com.asm.repository.ContractContainRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.ContractContainVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @param * @param null
 * @author Tanzujie
 * @date 2019-07-10
 * @return
 */
@Service
public class ContractContainService {
    @Autowired
    private ContractContainRepository contractContainRepository;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取合同包含列表
     *
     * @param page
     * @return
     */
    private List<ContractContainVo> getContractContainVoList(Page<ContractContain> page) {
        List<ContractContainVo> list = new ArrayList<>();
        for (ContractContain ContractContain : page.getContent()) {
            ContractContainVo contractContainVo = new ContractContainVo();
            BeanUtils.copyProperties(ContractContain, contractContainVo);
            list.add(contractContainVo);
        }
        return list;
    }

    /**
     * 分页查询所有合同包含
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<ContractContainVo> findAllContractContains(Integer pageNumber, Integer pageSize) {
        Page<ContractContain> page = contractContainRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<ContractContainVo>(pageNumber, pageSize, page.getTotalPages(), getContractContainVoList(page));
    }

    /**
     * 根据id查询合同包含
     *
     * @param id
     * @return
     */
    public ContractContainVo findContractContainById(Long id) {
        ContractContain ContractContain = contractContainRepository.findById(id).get();
        ContractContainVo contractContainVo = new ContractContainVo();
        BeanUtils.copyProperties(ContractContain, contractContainVo);
        return contractContainVo;
    }

    /**
     * 根据合同id分页查询合同包含
     *
     * @param
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<ContractContainVo> findContractContainByContractId(Long contractId, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<ContractContain> contractContains = contractContainRepository.findByContractId(contractId, page);
        return new PageResult<ContractContainVo>(pageNumber, pageSize, contractContains.getTotalPages(), getContractContainVoList(contractContains));
    }

    /**
     * 根据合广告位id分页查询合同包含
     *
     * @param size
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<ContractContainVo> findContractContainBySpotId(Long size, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<ContractContain> contractContains = contractContainRepository.findBySpotId(size, page);
        return new PageResult<ContractContainVo>(pageNumber, pageSize, contractContains.getTotalPages(), getContractContainVoList(contractContains));
    }

    /**
     * 添加合同包含
     *
     * @param contractContainVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void addContractContain(ContractContainVo contractContainVo) {
        ContractContain ContractContain = new ContractContain();
        BeanUtils.copyProperties(contractContainVo, ContractContain);
        ContractContain.setId(snowflakeIdWorker.nextId());
        ContractContain.setCreateBy(1L);
        ContractContain.setCreateAt(new Date());
        ContractContain.setUpdateBy(1L);
        ContractContain.setUpdateAt(new Date());
        contractContainRepository.save(ContractContain);
    }

    /**
     * 更新合同包含
     *
     * @param contractContainVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateContractContain(ContractContainVo contractContainVo) {
        ContractContain ContractContain = contractContainRepository.findById(contractContainVo.getId()).get();
        BeanUtils.copyProperties(contractContainVo, ContractContain);
        ContractContain.setUpdateBy(1L);
        ContractContain.setUpdateAt(new Date());
        contractContainRepository.save(ContractContain);
    }

    /**
     * 根据Id删除合同包含
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractContain(Long id) {
        contractContainRepository.deleteById(id);
    }
}
